package com.baizan.daka.ui.activity

import android.content.Context
import android.content.Intent
import android.text.InputType
import android.view.View
import com.baizan.base.BaseActivity
import com.baizan.daka.Constants
import com.baizan.daka.showToast
import com.baizan.daka.ui.contract.ModifyMoneyPasswordContract
import com.baizan.daka.ui.presenter.ModifyMoneyPasswordPresenter
import com.baizan.daka.util.StringMatches
import com.dianzan.daka.R
import kotlinx.android.synthetic.main.act_modify_money_password.*
import kotlinx.android.synthetic.main.act_modify_password.line1
import kotlinx.android.synthetic.main.act_modify_password.submit_bt

class ModifyMoneyPasswordActivity : BaseActivity<ModifyMoneyPasswordPresenter>(), ModifyMoneyPasswordContract.View {
    var isAdd :Boolean = true
    companion object {

        fun start(context: Context) {
            val intent = Intent(context, ModifyMoneyPasswordActivity::class.java)
            context.startActivity(intent)

        }
    }
    override fun initPresenter(): ModifyMoneyPasswordPresenter {
        return ModifyMoneyPasswordPresenter(this)
    }

    override fun layoutId(): Int {
        return R.layout.act_modify_money_password
    }
    private fun init() {
        if (isAdd) {
            line1.visibility = View.GONE
            passwordText1.visibility = View.GONE
            action_bar.setCenterText("设置资金密码")
            submit_bt.text="设置密码"
        } else {
            action_bar.setCenterText("修改资金密码")
            submit_bt.text="修改密码"
        }
        submit_bt.setOnClickListener {

            var pd1 = password1.text
            if (!isAdd && pd1.isEmpty()) {
                showToast("请输入旧密码")
                return@setOnClickListener
            }
            var pd2 = password2.text
            if (pd2.isEmpty()) {
                showToast("请输入新密码")
                return@setOnClickListener
            }
            var pd3 = password3.text
            if (pd3.isEmpty()) {
                showToast("请再次输入新密码")
                return@setOnClickListener
            }
            if (!pd2.toString().equals(pd3.toString())) {
                showToast("俩次输入新密码不一致")
                return@setOnClickListener
            }
            if (pd2.length != 6) {
                showToast("请输入6位数字密码")
                return@setOnClickListener
            }
            if (isAdd) {
                presenter?.addPassword(pd2.toString())
            } else {
                presenter?.modifyPassword(pd1.toString(), pd2.toString())
            }
        }
    }


    override fun start() {
        presenter?.getPassword()
    }


    override fun modifyPasswordSuccess() {
        showToast("修改密码成功")
        finish()
    }

    override fun addPasswordSuccess() {
        showToast("设置密码成功")
        finish()
    }

    override fun getPasswordSuccess(string: String) {
        isAdd = "1" != string
        init()
    }

    override fun initData() {
    }

    override fun initView() {
    }


}